<?php
/**
 * Created by PhpStorm.
 * Date: 2016/11/4
 * Time: 14:39
 * 打赏管理
 */

namespace Admin\Controller;


use Common\Controller\AdminbaseController;

class PropController extends AdminbaseController
{
    private $props = array(
        '1' => '双人PK打赏',
        '2' => '直播打赏',
        '3' => '多人打赏',
        '4' => '多人PK打赏',
    );
    /**
     * 打赏管理首页
     */
    public function index(){
        $live_id = I('live_id');
        $live = M("vrlive.live", null);
        $user_prop = M("vrlive.live_user_prop", null);
        $live_pk   = M("vrlive.live_pk");
        $pk_competitor = M("vrlive.live_pk_competitor");

        $currentPage = I('param.p', 0);
        if ($currentPage <= 0) {
            $currentPage = 1;
        }

        $pageSize = '20';
        $pageStart = ($currentPage - 1) * $pageSize;
        
        $live_pk_info = $live_pk->where("live_id = '".$live_id."' and (status = 1 or status = 2)")->order('create_time asc')->limit($pageStart, $pageSize)->select(); //pk列表
        $live_pk_count = $live_pk->where("live_id = '".$live_id."' and (status = 1 or status = 2)")->order('create_time asc')->count(); //pk总数
        if($live_pk_info){
            foreach ($live_pk_info as $key=>$val){
                $competitor_info = $pk_competitor->where(array('live_id'=>$live_id, 'pk_id'=>$val['id']))->select(); //pk组选手
                if($competitor_info){
                    foreach ($competitor_info as $k=>$v){
                        $data = $user_prop->where(array('live_id'=>$live_id, 'pk_id'=>$val['id'], 'competitor_id'=>$v['competitor_id']))->field('count(id) as num, sum(sentiment_value) as popular')->find();
                        $competitor_info[$k]['num'] = $data['num'];
                        $competitor_info[$k]['popular'] = $data['popular'];
                    }
                }
                $live_pk_info[$key]['competitor'] = $competitor_info;
            }
        }
        $live_info = $live->where(array('live_id'=>$live_id))->field('is_props')->find(); //直播打赏类型
        $user_prop_info = $user_prop->where(array('live_id'=>$live_id))->field('count(id) as total, sum(sentiment_value) as popular')->find();  //打赏总数
        $page = $this->page($live_pk_count ,20);
        $this->assign("page", $page->show('Admin'));
        $this->assign('title', $this->props[$live_info['is_props']]);
        $this->assign('prop_info', $user_prop_info);
        $this->assign('pk_info', $live_pk_info);
        $this->assign('live_id', $live_id);
        $this->display('index');
    }

    /**
     * 添加pk选手页面
     */
    public function add(){
        $live_id = I("live_id");
        $page    = I("page", 1);
        $data = $this->pages($page,1);
        $count = $data['count'];
        $totalPage = $data['totalPage'];
        $competitor_list = $data['list'];
        $this->assign("live_id", $live_id);
        $this->assign("page", $page);
        $this->assign("count", $count);
        $this->assign("totalPage", $totalPage);
        $this->assign("competitor_list", $competitor_list);
        $this->display('add');
    }

    public function pages($page, $type){
        $page    = I("page", $page);
        $group   = I("group");
        if($group == '1'){
            $name = 'competitor';
        }elseif($group == '2'){
            $name = 'competitor1';
        }
        if ($page <= 0) {
            $page = 1;
        }
        $pageSize  = '10';
        $pageStart = ($page - 1) * $pageSize;
        $competitor= M("vrlive.live_competitor");
        $count = $competitor->where(array('status'=>1))->count();
        $totalPage=ceil($count/$pageSize); //计算出总页数
        $competitor_list = $competitor->where(array('status'=>1))->limit($pageStart, $pageSize)->select();  //所有选手列表
        $result['count'] = $count;
        $result['totalPage'] = $totalPage;
        $result['list']      = $competitor_list;
        if($type == 1){
            return $result;
        }else if($type == 2){
            $str = '';
            $str .= '<table class="table table-hover table-bordered" style="width: 50%;">';
            $str .= '<thead>';
            $str .= '<tr>';
            $str .= '<th>选择</th>';
            $str .= '<th>序号</th>';
            $str .= '<th>姓名</th>';
            $str .= '<th>性别</th>';
            $str .= '<th>绰号</th>';
            $str .= '</tr>';
            $str .= '</thead>';
            $str .= '<tbody>';
            foreach($competitor_list as $key=>$val) {
                if ($val['sex'] == 1) {
                    $sex = '男';
                } else if ($val['sex'] == 2) {
                    $sex = '女';
                }
                $k = $key+1;
                $str .= '<tr>';
                $str .= "<td><input type='radio' name='".$name."' id='".$name."_".$val['competitor_id']."' value='".$val['competitor_id']."' onchange='change(".$val["competitor_id"].",".$group.")'></td>";
                $str .= '<td>'.$k.'</td>
                <td id="id_'.$val['competitor_id'].'">'.$val['real_name'].'</td>
                <td>'.$sex.'</td>
                <td>'.$val['nickname'].'</td></tr>';
            }
            $str .= '<tr><td colspan="5" style="font-size: 16px;">';
            if ($page != 1) { //页数不等于1
                $str .= '<a href="#" onclick="page(' . ($page - 1) . ','.$group.')">上一页</a>';
            }
            for ($i=1;$i<=$totalPage;$i++) {  //循环显示出页面
                $str .= '<a href="#" onclick="page(' . $i . ','.$group.')">'.$i.'</a>';
            }
            if ($page<$totalPage) { //如果page小于总页数,显示下一页链接
                $str .= '<a href="#" onclick="page('.($page+1).','.$group.')">下一页</a>';
            }
            $str .= '</td>
            </tr>
            <input type="hidden" value="'.$page.'" id="page">
            </tbody>
            </table>';
            echo $str;exit();
        }
    }

    /**
     * 选手信息入库
     */
    public function add_post(){
        $title     = I("title");
        $red_name  = I("red");
        $red_id    = I("red_id");
        $blue_name = I("blue");
        $blue_id   = I("blue_id");
        $live_id   = I("live_id");
        $pk = M("vrlive.live_pk");
        if(!$red_name && !$blue_name){
            $this->error('请填写PK双方信息');
        }else if(!$red_name && $blue_name){
            $this->error('请填写红方选手信息');
        }else if(!$blue_name && $red_name){
            $this->error('请填写蓝方选手信息');
        }
        $pk_data['id'] = time().$this->getRange();
        $pk_data['title']   = $title;
        $pk_data['live_id'] = $live_id;
        $pk_data['create_time'] = date('Y-m-d H:i:s', time());
        $pk_data['status'] = 1;
        $pk->add($pk_data);
        $pk_competitor = M("vrlive.live_pk_competitor");
        $data_list[] = array('competitor_id'=>$red_id, 'competitor_name'=>$red_name, 'pk_id'=>$pk_data['id'], 'live_id'=>$live_id, 'group_id'=>1);
        $data_list[] = array('competitor_id'=>$blue_id, 'competitor_name'=>$blue_name, 'pk_id'=>$pk_data['id'], 'live_id'=>$live_id, 'group_id'=>2);
        $pk_competitor->addAll($data_list);
        $this->success('发布成功！', U("Admin/Prop/index",array('live_id'=>$live_id)));
    }

    /**
     * 修改pk组标题
     */
    public function editTitle(){
        $pk_id = I('pk_id');
        $title = I("title");
        $pk = M("vrlive.live_pk");
        $pk_info = $pk->where(array('id'=>$pk_id))->find();
        if(!$pk_info){
            echo $this->ajaxReturn(array('code'=>201, 'msg'=>'pk组不存在'), 'JSON');
        }
        $data['title'] = $title;
        $pk->where(array('id'=>$pk_id))->save($data);
        echo $this->ajaxReturn(array('code'=>200, 'msg'=>'修改成功'), 'JSON');
    }

    /**
     * 修改pk组状态
     */
    public function edit_status(){
        $pk_id = I('pk_id');
        $status= I("status");
        $pk = M("vrlive.live_pk");
        $pk_info = $pk->where(array('id'=>$pk_id))->find();
        if(!$pk_info){
            if($status == 3){
                return $this->error('pk组不存在');
            }else{
                echo '2';
            }
        }
        $data['status'] = $status;
        $result = $pk->where(array('id'=>$pk_id))->save($data);
        if ($result) {
            if($status == 3){
                return $this->success('删除成功');
            }else{
                echo '1';
            }
        } else {
            if($status == 3){
                return $this->error($result['Message']);
            }else{
                echo '2';
            }
        }
    }
}